home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / bin / win_echod.py < prev    next >
Encoding:
Python Source  |  2006-05-17  |  1.9 KB  |  60 lines

  1. import win32file, win32pipe, pywintypes
  2.  
  3. PIPE = r"\\.\pipe\echo"
  4. BUFSIZE = 512
  5.  
  6. class Iocp:
  7.     def __init__(self, object):
  8.         self.port = win32file.CreateIoCompletionPort(-1, 0, 0, 0)
  9.         win32file.CreateIoCompletionPort(object.handle, self.port, 1, 0)
  10.  
  11.     def wait_buggy(self):
  12.         win32file.GetQueuedCompletionStatus(self.port, -1)
  13.  
  14.     def wait_good(self):
  15.         # keep a reference to the overlapped object
  16.         self.result = win32file.GetQueuedCompletionStatus(self.port, -1)[3]
  17.  
  18. class PipeService:
  19.     def __init__(self):
  20.         self.handle = win32pipe.CreateNamedPipe(PIPE,
  21.                           win32pipe.PIPE_ACCESS_DUPLEX|
  22.                           win32file.FILE_FLAG_OVERLAPPED,
  23.                           win32pipe.PIPE_TYPE_MESSAGE|
  24.                           win32pipe.PIPE_READMODE_MESSAGE|
  25.                           win32pipe.PIPE_WAIT,
  26.                           1, BUFSIZE, BUFSIZE,
  27.                           win32pipe.NMPWAIT_WAIT_FOREVER,
  28.                           None)
  29.         win32pipe.ConnectNamedPipe(self.handle, None)
  30.  
  31.     def serve(self):
  32.         print "Got connection"
  33.         win32file.WriteFile(self.handle, 'Hello!\n')
  34.         while 1:
  35.             data = win32file.ReadFile(self.handle, BUFSIZE)[1]
  36.             print "Got data: %r" % data
  37.             if not data[:4] == 'tran':
  38.                 win32file.WriteFile(self.handle, data)
  39.             print "Sent data"
  40.             if data[:4] == 'quit':
  41.                 break
  42.  
  43.     def __del__(self):
  44.         win32pipe.DisconnectNamedPipe(self.handle)
  45.  
  46. if __name__ == '__main__':
  47.     import sys
  48.     if 's' in sys.argv:
  49.         svc = PipeService()
  50.         iocp = Iocp(svc)
  51.         if 'bug' in sys.argv:
  52.             iocp.wait_buggy()
  53.         else:
  54.             iocp.wait_good()
  55.         svc.serve()
  56.     elif 'c' in sys.argv:
  57.         print win32pipe.CallNamedPipe(PIPE, "Hello there", BUFSIZE, 0)
  58.  
  59.         
  60.